package com.kumi.core.domain;

//import static com.kumi.core.constant.Constants.DYNAMIC_SUFFIX;
//import static com.kumi.core.constant.Constants.IDENTITY_COOKIE_NAME;
//import static com.kumi.core.constant.Constants.SITE_PREFIX;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.Type;
import org.springframework.web.util.WebUtils;

//import com.kumi.core.support.ForeContext;

/**
 * Site
 *
 * @author liufang
 *
 */
@Entity
@Table(name = "cms_site")
public class Site implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 模型类型
     */
    public static final String MODEL_TYPE = "site";
    /**
     * 正常状态
     */
    public static final int NORMAL = 0;
    /**
     * 禁用状态
     */
    public static final int DISABELD = 1;
    /**
     * 树编码长度
     */
    public static final int TREE_NUMBER_LENGTH = 4;

    public static String getIdentityCookie(HttpServletRequest request,
                                           HttpServletResponse response) {
        String value;
////		Cookie cookie = WebUtils.getCookie(request, IDENTITY_COOKIE_NAME);
//		if (cookie != null && StringUtils.isNotBlank(cookie.getValue())) {
//			value = cookie.getValue();
//		} else {
        value = UUID.randomUUID().toString();
//			value = StringUtils.remove(value, '-');
//			cookie = new Cookie(IDENTITY_COOKIE_NAME, value);
//			String ctx = request.getContextPath();
//			if (StringUtils.isBlank(ctx)) {
//				ctx = "/";
//			}
//			cookie.setPath(ctx);
//			cookie.setMaxAge(Integer.MAX_VALUE);
//			response.addCookie(cookie);
//		}
        return value;
    }

    @Transient
    public static String long2hex(long num) {
        BigInteger big = BigInteger.valueOf(num);
        String hex = big.toString(Character.MAX_RADIX);
        return StringUtils.leftPad(hex, TREE_NUMBER_LENGTH, '0');
    }

    @Transient
    public static long hex2long(String hex) {
        BigInteger big = new BigInteger(hex, Character.MAX_RADIX);
        return big.longValue();


    }

    @Transient
    public Object getConf(String className) {
        try {
            return Class.forName(className).getConstructor(Site.class)
                    .newInstance(this);
        } catch (Exception e) {
            throw new IllegalArgumentException("Class '" + className
                    + "' is not Conf Class", e);
        }
    }

    @Transient
    public <T> T getConf(Class<T> clazz) {
        try {
            return clazz.getConstructor(Site.class).newInstance(this);
        } catch (Exception e) {
            throw new IllegalArgumentException("Class '" + clazz.getName()
                    + "' is not Conf Class", e);
        }
    }


    private Integer id;
    private List<Site> children = new ArrayList<Site>(0);

    private Map<String, String> customs = new HashMap<String, String>(0);
    private Map<String, String> clobs = new HashMap<String, String>(0);




    private String number;


    @Column(name = "f_number", nullable = false, unique = true, length = 100)
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Id
    @Column(name = "f_site_id", unique = true, nullable = false)
    @TableGenerator(name = "tg_cms_site", pkColumnValue = "cms_site", table = "t_id_table", pkColumnName = "f_table", valueColumnName = "f_id_value", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "tg_cms_site")
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


}
